import 'package:background_downloader/background_downloader.dart';
import 'package:flutter/material.dart';
import 'package:top_foundation/top_foundation.dart';
import 'package:top_foundation/utils/theme/button.dart';
import 'package:top_foundation/utils/theme/theme.dart';
import 'package:whale/helper/component/download/state.dart';
import 'package:whale/helper/utils/util.dart';

import '../../../../helper/component/download/controller.dart';
import '../controller.dart';

class FileCell extends GetView<MutiDownload_2Controller> {
  const FileCell({
    super.key,
    required this.model,
  });
  final DownloadModel model;

  void onState(DownloadModel model) async {
    await DownloadController.instance.anyway(model);
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        if (controller.state.isEdit) {
          controller.onCellChoose(model);
        } else {
          DownloadController.instance.open(model);
        }
      },
      child: Container(
        decoration: BoxDecoration(border: Border(bottom: ITheme.borderSide)),
        padding: EdgeInsets.all(10.r),
        child: Row(
          children: [
            GetBuilder<MutiDownload_2Controller>(
              id: '${model.task.taskId}-choose',
              builder: (controller) {
                if (controller.state.isEdit) {
                  bool isChoosed = controller.state.editList.contains(model);

                  return IButton.icon(
                    onPressed: () => controller.onCellChoose(model),
                    icon: Icon(
                      isChoosed ? Icons.check_circle : Icons.circle_outlined,
                      size: 25.r,
                      color:
                          isChoosed ? ITheme.selectedColor : ITheme.borderColor,
                    ),
                    width: 40.r,
                  );
                }
                return const SizedBox();
              },
            ),
            Container(
              width: 50.r,
              height: 50.r,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(30.r),
              ),
              child: Center(
                child: Icon(
                  Icons.insert_drive_file,
                  size: 30.r,
                  color: ITheme.selectedColor,
                ),
              ),
            ),
            SizedBox(width: 10.r),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Row(
                    children: [
                      Text(
                        model.task.displayName,
                        style: ITheme.text.titleMedium,
                      ),
                      Obx(
                        () => model.status.value == TaskStatus.running
                            ? Text(
                                '（还有${model.timeRemaining}）',
                                style: ITheme.text.titleMedium,
                              )
                            : const SizedBox(),
                      ),
                    ],
                  ),
                  const SizedBox(height: 6),
                  Obx(
                    () => LinearProgressIndicator(
                      value: model.progress.value,
                      color: ITheme.selectedColor,
                      backgroundColor: ITheme.disableBgColor,
                      minHeight: 2,
                    ),
                  ),
                  const SizedBox(height: 3),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Obx(() => Text(
                            model.progress.value == 1
                                ? model.totalSize.fileSize
                                : '${model.downSize.value.fileSize}/${model.totalSize.fileSize}',
                            style: TextStyle(
                                color: ITheme.contentColor, fontSize: 13.sp),
                          )),
                      Obx(() => Text(
                            model.status.value == TaskStatus.running
                                ? model.speed.value
                                : '',
                            style: TextStyle(
                                color: ITheme.contentColor, fontSize: 13.sp),
                          )),
                    ],
                  ),
                ],
              ),
            ),
            Obx(() => IButton.text(
                  onPressed: () {
                    onState(model);
                  },
                  text: stateText,
                  width: 60.r,
                )),
          ],
        ),
      ),
    );
  }

  String get stateText {
    if (model.progress.value == 1) {
      return '查看';
    }
    return model.stateText.value;
  }
}
